package com.itdct.onflow.core.util


import com.itdct.onflow.core.extend.logError
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

/**
 * @author DCTANT
 * @version 1.0
 * @date 2021/11/4 14:50:58
 * @description
 */
object TimeUtil {
    const val DAY_TIME_STAMP: Long = 86400000L
    const val yyyyMMdd: String = "yyyyMMdd"
    const val yyyyMMddHHmmssZz: String = "yyyy-MM-dd HH:mm:ss Zz"
    const val yyyyMMddHHmmss: String = "yyyy-MM-dd HH:mm:ss"
    const val yyyyMMddHHmmss_HYPHEN: String = "yyyy-MM-dd HH:mm:ss"

    fun dateToTimeString(date: Date?): String {
        val simpleDateFormat = SimpleDateFormat(yyyyMMdd)
        val format = simpleDateFormat.format(date)
        return format
    }

    fun dateToTimeString(date: Date?, dateFormat: String?): String {
        val simpleDateFormat = SimpleDateFormat(dateFormat)
        val format = simpleDateFormat.format(date)
        return format
    }

    fun timestampToString(timestamp: Long, dataFormat: String?): String {
        val simpleDateFormat = SimpleDateFormat(dataFormat)
        val format = simpleDateFormat.format(Date(timestamp))
        return format
    }

    fun timeStringToDate(dateString: String?, dateFormat: String?): Date? {
        val simpleDateFormat = SimpleDateFormat(dateFormat)
        var date: Date? = null
        try {
            date = simpleDateFormat.parse(dateString)
        } catch (e: ParseException) {
            logError("时间转换出错", e)
        }
        return date
    }
}
